package com.example.admin.scheduler;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SchedulerX2Runnable implements Runnable{

    private static final Logger logger = LoggerFactory.getLogger(SchedulerX2Runnable.class);

    @Override
    public void run() {
        try {
            WebSocketClient webSocketClient = new WebSocketClient(new URI("ws://localhost:8110/websocket")) {
                @Override
                public void onOpen(ServerHandshake handshakedata) {
                    logger.info("连接已打开");
                    // 连接成功后发送消息
                    this.send("你好，WebSocket 服务器！ 欢迎来到 WebSocket 世界！");
                }

                @Override
                public void onMessage(String message) {
                    logger.info("收到消息: {}", message);
                }

                @Override
                public void onClose(int code, String reason, boolean remote) {
                    logger.info("连接已关闭: {} - {}", reason, code);
                }

                @Override
                public void onError(Exception ex) {
                    ex.printStackTrace();
                    logger.error("发生错误: {}", ex.getMessage());
                }
            };
            webSocketClient.connect();
        } catch (URISyntaxException e) {
            logger.error("URI 语法错误: {}", e.getMessage());
        }
        logger.info("SchedulerX2Runnable 定时任务: {}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }
}
